#include "ui_bofugui.h"
#include "bofugui.h"


/********************************************************************************************/
//6


void BofuGui::on_pushButton_CreateModuleOrder_clicked()
{
    if (ui->lineEdit_LanguageCode->text().isEmpty()){
        QMessageBox::warning(this, (QString::fromUtf8("Error")),
                             QString::fromUtf8("<h2>Missing information</h2>"
                                               "<p>Please fill in the language code.</p>"));
    } else {
        logDate();
        QFile in(exe.absolutePath()+"/.data/boss/module_order.example");  //Examplefile
        QFile out(""+dir.absolutePath()+"/boss_sampa/module_order.txt");   //Destinationfile

        QString line;
        if ( in.open(QIODevice::ReadOnly)&&out.open(QIODevice::WriteOnly)) {
            // files opened successfully
            QTextStream instream( &in );        // use a text stream
            QTextStream outstream( &out );        // use a text stream

            // until end of file...
            while ( !instream.atEnd()) {
                // read and parse a line
                line = instream.readLine();
                // do something with the line
                line.replace(QRegExp("Languagecode"), ui->lineEdit_LanguageCode->text());
                // std::cout << line.toUtf8().data() << std::endl;
                // write the output
                outstream << line.toUtf8().data() << "\n";
            }

            // Close the files
            in.close();
            out.close();

            writeToLog("echo \"module_order.txt created\" >> "+dir.absolutePath()+"/"+korpusname+".log");
            scriptDone();
        }
    }
}

void BofuGui::on_pushButton_EditModuleOrder_clicked()
{
    command=("cd "+dir.absolutePath()+"/boss_sampa/; touch module_order.txt");
    commandexec = system(command.toAscii().data());
    openURL(QUrl( dir.path().append("/boss_sampa/module_order.txt") ));

}

/********************************************************************************************/
//7

void BofuGui::on_pushButton_preselectionFolder_clicked()
{
    openURL(QUrl( ""+dir.absolutePath()+"/boss_sampa/"));
}

void BofuGui::on_pushButton_CreatePreselection_clicked()
{
  logDate();
  command = "cp "+exe.absolutePath()+"/.data/boss/*.txt "+dir.absolutePath()+"/boss_sampa/ >> "+dir.absolutePath()+"/"+korpusname+".log";
  commandexec = system(command.toAscii().data());
  ui->statusBar->showMessage(tr("Preselection-files created"),5000);
  writeToLog("echo \"Preselection-files created\" >> "+dir.absolutePath()+"/"+korpusname+".log");
  scriptDone();
}

/********************************************************************************************/
//8

void BofuGui::on_pushButton_CreateCartTrain_clicked()
{
    if (ui->lineEdit_Username->text().isEmpty() || ui->lineEdit_Password->text().isEmpty() || ui->lineEdit_InventoryName->text().isEmpty() || ui->lineEdit_LanguageCode->text().isEmpty()){
        QMessageBox::warning(this, (QString::fromUtf8("Error")),
                             QString::fromUtf8("<h2>Missing information</h2>"
                                               "<p>Please fill in Inventoryname, Languagecode, BOSS-user and User-Password.</p>"));
    } else{

        ui->statusBar->showMessage(tr("Creating Traindata...please wait!!!"),5000);
        logDate();
        command = "cd "+dir.absolutePath()+"; perl  "+bofu.absolutePath()+"/cartprep/cartprep.pl -lang "+ui->lineEdit_LanguageCode->text()+" -sp "+ui->lineEdit_InventoryName->text()+" -user "+ui->lineEdit_Username->text()+" -passwd "+ui->lineEdit_Password->text()+"  >> "+korpusname+".log 2>&1";
        commandexec = system(command.toAscii().data());
        scriptDone();
    }
}


void BofuGui::on_pushButton_cartTrainfile_clicked()
{
    command=("cd "+dir.absolutePath()+"/; touch traindata.txt");
    commandexec = system(command.toAscii().data());
    openURL(QUrl( dir.path().append("/traindata.txt") ));
}

void BofuGui::on_pushButton_CreateCartDescription_clicked()
{
    logDate();
    copyFile(exe.absolutePath()+"/.data/boss/desc",dir.absolutePath()+"/desc.txt");
    ui->statusBar->showMessage(tr("desc-file created"),5000);
    writeToLog("echo \"desc-file created\" >> "+dir.absolutePath()+"/"+korpusname+".log");

    scriptDone();
}

void BofuGui::on_pushButton_openCartDescription_clicked()
{
    command=("cd "+dir.absolutePath()+"/; touch desc.txt");
    commandexec = system(command.toAscii().data());
    openURL(QUrl( dir.path().append("/desc.txt") ));
}

void BofuGui::on_pushButton_wagon_clicked()
{
        ui->statusBar->showMessage(tr("Training...please wait!!!"),5000);
        logDate();
        command = "cd "+dir.absolutePath()+"/boss_sampa; wagon -data ../traindata.txt -desc ../desc.txt -output durcart.lsp -stop "+ui->codeBox_Cart->text().toAscii().data()+" >> ../"+korpusname+".log 2>&1";
        commandexec = system(command.toAscii().data());
        writeToLog("echo \"durcart.lsp created\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        scriptDone();
}
